<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
	<script>
		let obj = {
			name: 'mrc',
			age: 21,
			getName: function() {
				// this 指向obj
				console.log(this)
				console.log(this.name)
			},
			getAge: () => {
				// 箭头函数this 指向window
				console.log(this)
			}
		};
		obj.getName();
		obj.getAge();
		console.log(obj);

		// 构造函数
		var doSomething = function() {};
		doSomething.prototype.foo = "bar";
		console.log(doSomething.prototype);

		var newDoSomething = new doSomething();
		newDoSomething.prop = "new value";
		console.log(newDoSomething)


		function Person(name, age, num) {
			this.name = name;
			this.age = age;
			this.num = num;

			this.getName = function() {
				console.log(this.name);
			}
		}

		let person = new Person('desc', 11, 1);
		console.log(person)
		// 访问Person 构造函数
		console.log(person.__proto__)

		// create 使用传入对象的原型(__proto__)创建一个对象	
		let person2 = Object.create(person);
		person2.name = 'test';
		person2.age = 12;
		person2.num = 1;

		console.log(person2)

		// 可以使用constructor 获得构造函数，配合new 关键字即可调用
		let person3 = new person.constructor('constructor', 12, 3);
		console.log(person3)

		// 通过原型添加新的方法,动态改变到所有方法中
		Person.prototype.newMethod = function() {
			console.log('i am newMethod name=', this.name);
		}
		person.newMethod();
		person2.newMethod();
	</script>
</html>
